package com.xnx3.banmahuishou.qishouApplet.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.entity.QishouLeaveRecord;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.SafetyUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.Order;

/**
 * 骑手端--请假相关
 * @author 王晓龙
 */
@Controller
@RequestMapping("/qishouApplet/qishouLeaveRecord/")
public class QishouLeaveRecordController extends BaseController {
	@Resource
	private SqlService sqlService;
	
	/**
	 * 请假申请
	 * @param startTime 请假开始时间 十位时间戳
	 * @param endTime 请假结束时间 十位时间戳
	 * @param reason 请假原因
	 * @author 王晓龙
	 */
	@RequestMapping(value = "addLeave.json")
	@ResponseBody
	public BaseVO getQishouOrderGoodsList(HttpServletRequest request,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime,
			@RequestParam(value = "reason",required = true,defaultValue = "") String reason) {
		//getUserId()
		int userId = getUserId();
		if (userId == 0) {
			return error("请登录");
		}
		//查询当前登录骑手的用户表信息
		User user = sqlService.findById(User.class, userId);
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		//查询骑手请假表是否有当前骑手正在审核中的请假记录
		String sqljilu = "SELECT * FROM laji_qishou_leave_record where qishou_userid=" + userId + " AND state=" + QishouLeaveRecord.STATE_UNDER;
		List<QishouLeaveRecord> listLeaveRecords = sqlService.findBySqlQuery(sqljilu, QishouLeaveRecord.class);
		if(!listLeaveRecords.isEmpty()) {
			return error("您的请假已经在审核中，请不要重复提交");
		}
		//查询骑手请假表是否有当前骑手已经通过审核待函数处理的请假记录
		String sqlchuli = "SELECT * FROM laji_qishou_leave_record where qishou_userid=" + userId + " AND state=" + QishouLeaveRecord.STATE_PASSED;
		List<QishouLeaveRecord> listchuli = sqlService.findBySqlQuery(sqlchuli, QishouLeaveRecord.class);
		if(!listchuli.isEmpty()) {
			return error("您已经有批准过的请假了");
		}
		//获取当前骑手的工作状态 如果已经在请假中给提示 工作中则是可以提交请假
		if(qishou.getState() == Qishou.STATE_LEAVE) {
			return error("您已经在请假中了");
		} else {
			if (startTime <= 0) {
				return error("请选择请假开始时间");
			}
			if (endTime <= 0) {
				return error("请选择请假结束时间");
			}
			if (endTime < startTime ) {
				return error("请假结束时间不能早于开始时间");
			}
			if(startTime < DateUtil.timeForUnix10()){
				return error("请假开始时间不能小于当前时间");
			}
			if(endTime < DateUtil.timeForUnix10()){
				return error("请假结束时间不能小于当前时间");
			}
			if (!StringUtils.hasLength(reason)) {
				return error("请填写请假原因");
			}
			String sql = "SELECT * FROM shop_order WHERE qishou_id = " + userId + " AND state = " + Order.STATE_DAIQUHUO + " AND ((reserve_start_time > " + startTime + " AND reserve_start_time < " + endTime + ") OR (reserve_end_time > " + startTime + " AND reserve_end_time < " + endTime + "))";
			List<Order> list = sqlService.findBySqlQuery(sql,Order.class);
			if(!list.isEmpty() || list.size() != 0) {
				return error("您有未取订单,请给管理员打电话进行协商");
			}
			//查询当前登录骑手的骑手表信息
			QishouLeaveRecord qishouLeaveRecord = new QishouLeaveRecord();
			qishouLeaveRecord.setWanggeUserid(qishou.getWanggeUserid());
			qishouLeaveRecord.setCreateTime(DateUtil.timeForUnix10());
			qishouLeaveRecord.setStartTime(startTime);
			qishouLeaveRecord.setEndTime(endTime);
			qishouLeaveRecord.setReason(SafetyUtil.xssFilter(reason));
			qishouLeaveRecord.setState(QishouLeaveRecord.STATE_UNDER);
			qishouLeaveRecord.setQishouUserid(getUserId());
			qishouLeaveRecord.setQishouUsername(user.getNickname());
			qishouLeaveRecord.setQishouPhone(user.getPhone());
			sqlService.save(qishouLeaveRecord);
			//日志记录
			ActionLogUtil.insertUpdateDatabase(request, userId, "骑手--请假申请 :" + qishouLeaveRecord.toString());
			return success();
		}
	}
}
